
//
//  RecommendCellBottomView.m
//  高仿MONO
//
//  Created by 徐茂怀 on 2018/4/25.
//  Copyright © 2018年 徐茂怀. All rights reserved.
//

#import "CommunicateBottomView.h"
#import "UIView+SDAutoLayout.h"

#define KMarginLeft     12.0
#define KMarginRight    12.0

@interface CommunicateBottomView()
{
    UIButton *_collectBtn;//收藏按钮
    UIImageView *_likeImageView;//收藏动画图片
    UIImageView *_dislikeImageView;//取消收藏动画图片
    NSMutableArray *_likeAnmations;//收藏动画
    NSMutableArray *_dislikeAnmations;//取消收藏动画
    UIView *_bottomView;
    UIView *_lineView;
    UIColor *_textColor;
    UIImage *_shareImage;
    UIImage *_collectImage;
    UIImage *_priaiseImage;
    UIImage *_communicateImage;
    UIImage *_backImage;
    UIImage *_moreImage;
}
@end
const NSInteger kButtonWidth = 48;
const NSInteger kButtonHeight = 50;
@implementation CommunicateBottomView

-(instancetype)initWithFrame:(CGRect)frame CommunicateType:(CommunicateBottomType)communicateType
{
    if(self = [super initWithFrame:frame]){
        [self setColorStyleWithCommunicateBottomType:communicateType];
        [self setupView];
        self.communicateBottomType = communicateType;
        [self clickButtonAction];
    }
    return self;
}
-(void)setCommunicateBottomType:(CommunicateBottomType)communicateBottomType
{
    switch (communicateBottomType) {
//        case CommunicateBottomTypeCellGray:
//            [self setViewByCommunicateBottomTypeCellGray];
//            break;
//        case CommunicateBottomTypeCellWhite:
//            [self setViewByCommunicateBottomTypeCellWhite];
//            break;
        case CommunicateBottomTypeDetailGray:
            [self setViewByCommunicateBottomTypeDetailGray];
            break;
//        case CommunicateBottomTypeDetailWhite:
//            [self setViewByCommunicateBottomTypeDetailWhite];
//            break;
        default:
            break;
    }
}

-(void)setColorStyleWithCommunicateBottomType:(CommunicateBottomType)commicateBottomType;
{
    switch (commicateBottomType) {
//        case CommunicateBottomTypeCellGray:
        case CommunicateBottomTypeDetailGray:
            _shareImage = [UIImage imageNamed:@"评论条分享"];
            _backImage = [UIImage imageNamed:@"btn-topback-active"];
            _moreImage = [UIImage imageNamed:@"点点点"];//icon-more-grey
            _priaiseImage = [UIImage imageNamed:@"点赞"];
            _priaiseSelectedImage = [UIImage imageNamed:@"点赞_sel"];
            _communicateImage = [UIImage imageNamed:@"留言"];

            _collectImage = [UIImage imageNamed:@"item-btn-like-black"];
            
            break;
//        case CommunicateBottomTypeCellWhite:
//        case CommunicateBottomTypeDetailWhite:
//            _shareImage = [UIImage imageNamed:@"item-btn-share-white"];
//            _collectImage = [UIImage imageNamed:@"item-btn-like-white"];
//            _priaiseImage = [UIImage imageNamed:@"item-btn-thumb-white"];
//            _communicateImage = [UIImage imageNamed:@"item-btn-comment-white"];
//            _moreImage = [UIImage imageNamed:@"icon-more-white"];
//            _priaiseSelectedImage = [UIImage imageNamed:@"item-btn-thumb-white-on"];
//            _backImage = [UIImage imageNamed:@"btn-top-backwhite"];
//            break;
        default:
            break;
    }
}

-(void)setupView{
    _lineView = [UIView new];
    _lineView.backgroundColor = [UIColor colorWithRed:0.95 green:0.95 blue:0.95 alpha:1];
    _bottomView = [UIView new];
    _bottomView.backgroundColor = [UIColor colorWithRed:0.86 green:0.89 blue:0.91 alpha:1];
    
    _shareBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [_shareBtn setImage:_shareImage forState:UIControlStateNormal];
    _collectBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [_collectBtn setImage:_collectImage forState:UIControlStateNormal];
    
    _backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [_backBtn setImage:_backImage forState:UIControlStateNormal];
    
    _likeAnmations = @[].mutableCopy;
    _dislikeAnmations = @[].mutableCopy;
    for (NSUInteger i = 1; i < 15; i++) {
        UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"liked_%zd", i]];
        [_likeAnmations addObject:image];
    }
    for (NSUInteger i = 1; i < 9; i++) {
        UIImage *image1 = [UIImage imageNamed:[NSString stringWithFormat:@"dislike_%zd", i]];
        [_dislikeAnmations addObject:image1];
    }
    _likeImageView = [UIImageView new];
    _likeImageView.contentMode = UIViewContentModeScaleAspectFit;
    _likeImageView.image = nil;
    _likeImageView.userInteractionEnabled = NO;
    _likeImageView.animationImages = _likeAnmations;
    _likeImageView.animationDuration = 0.3;//设置动画时间
    _likeImageView.animationRepeatCount = 1;
   
    _dislikeImageView = [UIImageView new];
    _dislikeImageView.contentMode = UIViewContentModeScaleAspectFit;
    _dislikeImageView.image = nil;
    _dislikeImageView.userInteractionEnabled = NO;
    _dislikeImageView.animationImages = _dislikeAnmations;
    _dislikeImageView.animationDuration = 0.3;//设置动画时间
    _dislikeImageView.animationRepeatCount = 1;
 
    _praiseBtn = [MCFireworksButton buttonWithType:UIButtonTypeCustom];
    [_praiseBtn setImage:_priaiseImage forState:UIControlStateNormal];
    _praiseBtn.titleLabel.font = FONT_(12);
    _praiseBtn.particleImage = [UIImage imageNamed:@"Sparkle"];
    _praiseBtn.particleScale = 0.05;
    _praiseBtn.particleScaleRange = 0.02;

    _communicateBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [_communicateBtn setImage:_communicateImage forState:UIControlStateNormal];
    _communicateBtn.titleLabel.font = FONT_(12);
    _moreBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [_moreBtn setImage:_moreImage forState:UIControlStateNormal];
    
    NSArray *views = @[_lineView,_shareBtn,_collectBtn,_praiseBtn,_communicateBtn,_moreBtn,_bottomView,_dislikeImageView,_likeImageView,_backBtn];
    [self sd_addSubviews:views];
    
    self.pingLunNumLabel = [[UILabel alloc] init];
    self.pingLunNumLabel.text = @"0";
    self.pingLunNumLabel.textColor = [UIColor whiteColor];
    self.pingLunNumLabel.font = [UIFont systemFontOfSize:11];
    self.pingLunNumLabel.backgroundColor = [UIColor colorWithRed:0.94 green:0.33 blue:0.34 alpha:1.00];
    self.pingLunNumLabel.textAlignment = NSTextAlignmentCenter;
    self.pingLunNumLabel.layer.masksToBounds = YES;
    // 设置圆角大小
    self.pingLunNumLabel.layer.cornerRadius = 6.0 ;
    
    [_communicateBtn addSubview:self.pingLunNumLabel];
    
    
    UIView *viewBackColor = [[UIView alloc]init];
    _viewBackColor = viewBackColor;
    [viewBackColor.layer setMasksToBounds:YES];
    [viewBackColor.layer setCornerRadius:12.5];//6
    viewBackColor.backgroundColor = RGBColor(243, 243, 243);
    [self addSubview:viewBackColor];
    
    UILabel *uilabel = [[UILabel alloc]init];
    _uilabel = uilabel;
    uilabel.text = @"写评论...";
    uilabel.enabled = NO;//lable必须设置为不可用 颜色问题
    uilabel.font = [UIFont systemFontOfSize:13];
    [self addSubview:uilabel];
    
    UIImageView *imageViewBi = [[UIImageView alloc] init];
    self.imageViewBi = imageViewBi;
    imageViewBi.image = [UIImage imageNamed:@"编辑"];
    [self addSubview:imageViewBi];

}
////cell灰色
//-(void)setViewByCommunicateBottomTypeCellGray
//{
//    _lineView.hidden = NO;
//    self.backgroundColor = [UIColor whiteColor];
//    [_praiseBtn setTitleColor:[UIColor colorWithRed:0.7 green:0.7 blue:0.7 alpha:1] forState:UIControlStateNormal];
//     [_communicateBtn setTitleColor:[UIColor colorWithRed:0.7 green:0.7 blue:0.7 alpha:1] forState:UIControlStateNormal];
//    _lineView.sd_layout.topEqualToView(self).leftSpaceToView(self, KMarginLeft).rightSpaceToView(self, KMarginRight).heightIs(0.5);
//    _shareBtn.sd_layout.leftEqualToView(self).topSpaceToView(_lineView, 0).widthIs(kButtonWidth).heightIs(kButtonHeight);
//    _collectBtn.sd_layout.leftSpaceToView(_shareBtn, 0).topEqualToView(_shareBtn).widthIs(kButtonWidth).heightIs(kButtonHeight);
//    _praiseBtn.sd_layout.leftSpaceToView(_collectBtn, 0).topEqualToView(_collectBtn).widthIs(kButtonWidth).heightIs(kButtonHeight);
//    [_praiseBtn setupAutoSizeWithHorizontalPadding:4 buttonHeight:kButtonHeight];
//    _communicateBtn.sd_layout.leftSpaceToView(_praiseBtn, 0).topEqualToView(_praiseBtn).widthIs(kButtonWidth).heightIs(kButtonHeight);
//    [_communicateBtn setupAutoSizeWithHorizontalPadding:4 buttonHeight:kButtonHeight];
//    _moreBtn.sd_layout.rightEqualToView(self).centerYEqualToView(_shareBtn).widthIs(4 * KMarginRight).heightIs(kButtonHeight);
//    _bottomView.sd_layout.leftEqualToView(self).rightEqualToView(self).topSpaceToView(_shareBtn, 0).heightIs(10);
//    _likeImageView.sd_layout.bottomEqualToView(_shareBtn).leftEqualToView(_collectBtn).widthIs(45).heightIs(90);
//    //55 54
//   _dislikeImageView.sd_layout.bottomEqualToView(_shareBtn).leftEqualToView(_collectBtn).widthIs(55).heightIs(54);
//}
////cell白色
//-(void)setViewByCommunicateBottomTypeCellWhite
//{
//    [self setViewByCommunicateBottomTypeCellGray];
//    _lineView.hidden = YES;
//    self.backgroundColor = [UIColor clearColor];
//    [_praiseBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
//    [_communicateBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
//}
//详情灰色
-(void)setViewByCommunicateBottomTypeDetailGray
{
    _lineView.hidden = NO;
    self.backgroundColor = [UIColor whiteColor];
    [_praiseBtn setTitleColor:[UIColor colorWithRed:0.7 green:0.7 blue:0.7 alpha:1] forState:UIControlStateNormal];
    [_communicateBtn setTitleColor:[UIColor colorWithRed:0.7 green:0.7 blue:0.7 alpha:1] forState:UIControlStateNormal];
    
    _lineView.sd_layout.topEqualToView(self).leftSpaceToView(self, 0).rightSpaceToView(self, 0).heightIs(0.5);
    //返回
    _backBtn.sd_layout.leftSpaceToView(self, 0).topEqualToView(_lineView).widthIs(kButtonHeight).heightIs(kButtonHeight);
    //更多
    _moreBtn.alpha = 0.7;
    _moreBtn.sd_layout.rightEqualToView(self).topSpaceToView(_lineView, 0).widthIs(0/**4 * KMarginRight*/).heightIs(kButtonHeight);
    //分享
//    _shareBtn.backgroundColor = arc4random_color;//调用随机色
    _shareBtn.sd_layout.rightSpaceToView(_moreBtn, 10).topSpaceToView(_lineView, 0).widthIs(kButtonWidth).heightIs(kButtonHeight);
//    [_shareBtn setupAutoSizeWithHorizontalPadding:20 buttonHeight:kButtonHeight];
    //点赞
    _praiseBtn.sd_layout.rightSpaceToView(_shareBtn, -5).topSpaceToView(_lineView, 0).widthIs(kButtonWidth).heightIs(kButtonHeight);
    //爱心收藏按钮
    _collectBtn.sd_layout.rightSpaceToView(_praiseBtn, -3).topSpaceToView(_lineView, 0).widthIs(0/**kButtonWidth*/).heightIs(kButtonHeight);
    //评论数
    _communicateBtn.sd_layout.rightSpaceToView(_collectBtn,5).topSpaceToView(_lineView, 0).widthIs(kButtonWidth).heightIs(kButtonHeight);
    [_pingLunNumLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(_communicateBtn.mas_centerY).offset(-5);
        make.width.greaterThanOrEqualTo(@(20));
        make.height.mas_equalTo(12);
        make.left.equalTo(_communicateBtn.mas_centerX);
    }];
    _imageViewBi.sd_layout.topSpaceToView(_lineView, 10+5).leftSpaceToView(_backBtn,10).widthIs(18).heightIs(18);
    _viewBackColor.sd_layout.topSpaceToView(_lineView, 10).leftSpaceToView(_backBtn,-5).rightSpaceToView(_communicateBtn , 5).bottomSpaceToView(self, 10);
    _uilabel.sd_layout.topSpaceToView(_lineView, 10+7).leftSpaceToView(_imageViewBi,8).heightIs(18);

    //收藏动画
    _likeImageView.sd_layout.bottomEqualToView(_communicateBtn).leftEqualToView(_collectBtn).widthIs(45).heightIs(90);
    //55 54
    _dislikeImageView.sd_layout.bottomEqualToView(_communicateBtn).leftEqualToView(_collectBtn).widthIs(55).heightIs(54);
}
//-(void)setViewByCommunicateBottomTypeDetailWhite
//{
//    [self setViewByCommunicateBottomTypeDetailGray];
//    _lineView.hidden = YES;
//    self.backgroundColor = [UIColor blackColor];
//    [_praiseBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
//    [_communicateBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
//}

-(void)clickButtonAction
{
    @weakify(self)
    
    [[_collectBtn rac_signalForControlEvents:UIControlEventTouchUpInside]subscribeNext:^(id x) {
        NSLog(@"收藏");
        @strongify(self)
        self.is_folded = !self.is_folded;
        if(self.is_folded){
            [self->_likeImageView startAnimating];
            [self->_collectBtn setImage:[UIImage imageNamed:@"item-btn-like-on"] forState:UIControlStateNormal];//item-btn-like-black
        }else{
            [self->_dislikeImageView startAnimating];
            [self->_collectBtn setImage:self->_collectImage forState:UIControlStateNormal];
        }
    }];
}

@end
